package com.example2016.cjj.servicedemo.MyDownloader;

import android.os.AsyncTask;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by CJJ on 2016/12/22.
 */

public class DownloadTask extends AsyncTask<String,Integer,Integer> {
    public static final int TYPE_SUCCESS = 0;
    public static final int TYPE_FAILED = 1;
    public static final int TYPE_PAUSED = 2;
    public static final int TYPE_CANCELED = 3;

    private DownloadListener downloadListener;

    private boolean isCanceled = false;

    private boolean isPaused = false;

    private int lastProgress=0;


    public DownloadTask(DownloadListener downloadListener) {
        this.downloadListener = downloadListener;
    }

    @Override
    protected Integer doInBackground(String... params) {
        InputStream is = null;
        RandomAccessFile savedFile = null;
        File file = null;


        try {

            long downloadedLength = 0;  //记录已下载的文件长度
            String downloadUrl = params[0];
            String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
            String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
            file = new File(directory+fileName);
            if(file.exists()){
                downloadedLength = file.length();
            }
            long contentLength = getContentLength(downloadUrl);
            if(contentLength==0){
                return TYPE_FAILED;
            }else if(contentLength==downloadedLength){
                //已下载字节和文件总字节相等，说明文件已经现在完成了。
                return TYPE_SUCCESS;
            }
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(downloadUrl)
                    .addHeader("RANGE","bytes="+downloadedLength+"-")
                    .build();
            Response response = okHttpClient.newCall(request).execute();
            if(response!=null && response.isSuccessful()){
                is = response.body().byteStream();
                savedFile= new RandomAccessFile(file,"rw");
                savedFile.seek(downloadedLength); //跳过已下载的字节
                byte[] buf =new byte[1024];
                int total = 0;
                int len;
                while((len=is.read(buf))!=-1){
                    if(isCanceled){
                        return TYPE_CANCELED;
                    }else if(isPaused){
                        return TYPE_PAUSED;
                    }else{
                        total +=len;
                        savedFile.write(buf,0,len);
                        //计算已下载的百分比
                        int progress = (int) ( (total+downloadedLength)*100/contentLength ) ;
                        publishProgress(progress);
                    }
                }
                response.close();
                return  TYPE_SUCCESS;

            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(is!=null){
                    is.close();
                }
                if(savedFile!=null){
                    savedFile.close();
                }
                if(isCanceled && file !=null){
                    file.delete();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return TYPE_FAILED;
    }


    @Override
    protected void onProgressUpdate(Integer... values) {
        int progress = values[0];
        if(progress > lastProgress){
            downloadListener.onProgress(progress);
            lastProgress = progress;
        }
    }

    @Override
    protected void onPostExecute(Integer status) {
        switch (status){
            case TYPE_SUCCESS:
                downloadListener.onSuccess();
                break;
            case TYPE_FAILED:
                downloadListener.onFailed();
                break;
            case TYPE_PAUSED:
                downloadListener.onPaused();
                break;
            case TYPE_CANCELED:
                downloadListener.onCanceled();
                break;
           default:
                break;
        }
    }


    public void pauseDownload(){
        isPaused=true;
    }

    public void cancelDownload(){
        isCanceled = true;
    }


    //获取服务器上，要下载文件的大小
    private long getContentLength(String downloadUrl) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(downloadUrl)
                .build();
        Response response = okHttpClient.newCall(request).execute();
        if(response!=null && response.isSuccessful()){
            long  contentLength =  response.body().contentLength();
            response.close();
            return contentLength;
        }

        return 0;
    }

}
